﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevices.Manager.Properties;

namespace Jbe.TestSuite.TestDevices.Manager.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private TestDevicesPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
        }


        public void Initialize()
        {
            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(Resources.TestDevicesMenu, Resources.DeviceManager.ToBitmap());
            menuItem.Click += (sender, e) => ShowTestDevicesView();
            regionManager.Regions[ShellRegionNames.ViewMenuItems].Add(menuItem);

            ShowTestDevicesView();
        }

        // INFO: This window cannot be closed because it defines a region.
        private void ShowTestDevicesView()
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<TestDevicesPresentationModel>();
                regionManager.Regions[ShellRegionNames.RightRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.RightRegion].Activate(presentationModel.View);
        }
    }
}
